package com.aiapphub.SystemCode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.aiapphub.BaseCode.utils.BeanCopyUtils;
import com.aiapphub.BaseCode.vo.PageVo;
import com.aiapphub.SystemCode.entity.SalaryStatement;
import com.aiapphub.SystemCode.mapper.SalaryMapper;
import com.aiapphub.SystemCode.service.SalaryService;
import com.aiapphub.SystemCode.vo.SalaryVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 工资明细表(SalaryStatement)表服务实现类
 *
 * @author makejava
 * @since 2025-04-19 15:51:14
 */


@Service("salaryService")
public class SalaryServiceImpl extends ServiceImpl<SalaryMapper, SalaryStatement> implements SalaryService {


    @Override
    public SalaryStatement getSalaryStatement(String accountPeriod, String jobNumber) {
        // 使用LambdaQueryWrapper进行查询，确保字段安全
        LambdaQueryWrapper<SalaryStatement> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SalaryStatement::getAccountPeriod, accountPeriod)  // 根据账期字段查询
                .eq(SalaryStatement::getJobNumber, jobNumber)  // 根据工号字段查询
                .eq(SalaryStatement::getStatus, 0);  // 当status=0时返回数据
        // 执行查询并返回结果，返回符合条件的唯一一条记录
        SalaryStatement result = this.getOne(lambdaQueryWrapper);
        System.out.println(result);
        return result;
    }

    @Override
    public PageVo selectSalaryPage(Integer pageNum, Integer pageSize) {
        // 构造查询条件
        LambdaQueryWrapper<SalaryStatement> queryWrapper = new LambdaQueryWrapper<>();
       
        // 分页查询数据
        Page<SalaryStatement> page = new Page<>(pageNum, pageSize);
        page(page, queryWrapper);

        // 将查询结果列表转换成分页对象，并返回给客户端
        List<SalaryVo> salaryVos = BeanCopyUtils.copyBeanList(page.getRecords(), SalaryVo.class);
        PageVo pageVo = new PageVo(salaryVos, page.getTotal());
        return pageVo;
    }

    @Override
    public boolean updateSalaryStatus(String accountPeriod, Integer status) {

        // 使用Lambda表达式 (更推荐)
         return this.lambdaUpdate()
                 .eq(SalaryStatement::getAccountPeriod, accountPeriod)
                 .set(SalaryStatement::getStatus, status)
                 .update();
    }

    @Override
    public PageVo selectCategoryPage(SalaryStatement salaryStatement, Integer pageNum, Integer pageSize) {
        // 构造查询条件
        LambdaQueryWrapper<SalaryStatement> queryWrapper = new LambdaQueryWrapper<>();

        // 模糊查询文件名称
        queryWrapper.like(StringUtils.hasText(salaryStatement.getAccountPeriod()), SalaryStatement::getAccountPeriod, salaryStatement.getAccountPeriod());

        // 模糊查询文件路径
        queryWrapper.like(StringUtils.hasText(salaryStatement.getDepartment()), SalaryStatement::getDepartment, salaryStatement.getDepartment());

        // 查询文件状态
        queryWrapper.eq(salaryStatement.getStatus() != null, SalaryStatement::getStatus, salaryStatement.getStatus());

        // 分页查询数据
        Page<SalaryStatement> page = new Page<>(pageNum, pageSize);
        page(page, queryWrapper);

        // 将查询结果列表转换成分页对象，并返回给客户端
        List<SalaryVo> salaryStatementVos = BeanCopyUtils.copyBeanList(page.getRecords(), SalaryVo.class);
        PageVo pageVo = new PageVo(salaryStatementVos, page.getTotal());
        return pageVo;
    }


}



